from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import sessionmaker
from passlib.apps import custom_app_context as pwd_context
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
import time

from flask_restful import reqparse, abort, Api, Resource, fields,\
    marshal_with
from flask_restful_swagger import swagger


app = Flask(__name__)
 #数据库url与名字类型配置
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:Wwwwccctv1@121.36.9.1:3306/MapDB'  
#root 用户 密码
app.config["SECRET_KEY"] = 'Wwwwccctv1' 
db = SQLAlchemy(app)


''' 
管理员表l
account  = 账户
password = 密码
isadmin = 权限等级
'''

@swagger.model
class Admin(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    account = db.Column(db.String(12), unique=True)
    password = db.Column(db.String(30), nullable=False)
    isadmin = db.Column(db.Integer, nullable=False)
    update_at = db.Column(db.String(120))
    create_at = db.Column(db.String(120))

    def __init__(self, account, password, isadmin, update_at, create_at):
        self.account = account
        self.hash_password(password)
        self.isadmin = isadmin
        self.update_at = update_at
        self.create_at = create_at
    def as_dict(self):
         return {
            "id": self.id,
            "account": self.account,
            "isadmin": self.isadmin,
            "update_at": self.update_at,
            "create_at": self.create_at,
        }
    #密码加密
    def hash_password(self, password):
        self.password = pwd_context.hash(password)
    #密码解密
    def verify_password(self, password):
        return pwd_context.verify(password, self.password)


'''
用户表
name = 用户名
head_picture = 头像url
phone = 手机号
examine =审核员
password = 密码
support = 点赞数
'''

@swagger.model
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    name = db.Column(db.String(80), unique=True)
    head_picture = db.Column(db.String(1024), unique=True)
    phone = db.Column(db.String(80), unique=True)
    examine = db.Column(db.Integer, nullable=False)
    password = db.Column(db.String(256), nullable=False)
    support = db.Column(db.Integer, nullable=False)
    last_login = db.Column(db.String(120))
    token = db.Column(db.String(1024))
    update_at = db.Column(db.String(120))
    create_at = db.Column(db.String(120))

    
    def __init__(self, name, head_picture, phone, examine,password, support,last_login,token,update_at, create_at):
        self.name = name
        self.head_picture = head_picture
        self.phone = phone
        self.examine = examine
        self.hash_password(password)
        self.support = support
        self.last_login = last_login
        self.set_token
        self.update_at = update_at
        self.create_at = create_at

    def as_dict(self):
        return {
            "id": self.id,
            "name": self.name,
            "head_picture": self.head_picture,
            "phone": self.phone,
            "examine": self.examine,
            "support": self.support,
            "token": self.token,
            "create_at":self.create_at,
        }
    
    def set_token(self):
        self.token = self.last_login+'#'+self.phone

    def hash_password(self, password):
        self.password = pwd_context.encrypt(password)

    def verify_password(self, password):
        return pwd_context.verify(password, self.password)


'''
资讯表
title = 标题
content = 内容
picture = 图片url
create_user_id = 创建人
support = 获赞数
unsupported = 获踩数
postcodes = 邮政编码 北京 = 100010
place = 地点
coordinate_x = x坐标
coordinate_y = y坐标
state = 状态  1为上线  0 为下线
'''

@swagger.model
class Information(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    title = db.Column(db.String(20))
    content = db.Column(db.String(120))
    picture = db.Column(db.String(256))
    create_user_id = db.Column(db.Integer, nullable=False)
    support = db.Column(db.Integer, nullable=False)
    unsupported = db.Column(db.Integer, nullable=False)
    postcodes = db.Column(db.Integer, nullable=False)
    place = db.Column(db.String(120), nullable=False)
    coordinate_x = db.Column(db.String(120), nullable=False)
    coordinate_y = db.Column(db.String(120), nullable=False)
    state = db.Column(db.Integer)
    update_at = db.Column(db.String(120))
    create_at = db.Column(db.String(120))

    def __init__(self, title, content, picture, create_user_id, support,
                 unsupported, postcodes, place, coordinate_x, coordinate_y, state, update_at, create_at):
        self.title = title
        self.content = content
        self.picture = picture
        self.create_user_id = create_user_id
        self.support = support
        self.unsupported = unsupported
        self.postcodes = postcodes
        self.place = place
        self.coordinate_x = coordinate_x
        self.coordinate_y = coordinate_y
        self.state = state
        self.update_at = update_at
        self.create_at = create_at

    def as_dict(self):
        return {
            "id": self.id,
            "title": self.title,
            "content": self.content,
            "picture": self.picture,
            "create_user_id": self.create_user_id,
            "support": self.support,
            "unsupported": self.unsupported,
            "postcodes": self.postcodes,
            "place": self.place,
            "state": self.state,
            "coordinate_x": self.coordinate_x,
            "coordinate_y": self.coordinate_y,
            "create_at": self.create_at,
        }


'''
评论表
content = 内容
information_id = 资讯id
comment_user_id = 评论的用户id
create_user_id = 资讯发布者id
'''

@swagger.model
class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    content = db.Column(db.String(120))
    information_id = db.Column(db.Integer, nullable=False)
    comment_user_id = db.Column(db.Integer, nullable=False)
    create_user_id = db.Column(db.Integer, nullable=False)
    update_at = db.Column(db.String(120))
    create_at = db.Column(db.String(120))

    def __init__(self, content, information_id, comment_user_id,create_user_id,update_at, create_at):
        self.content = content
        self.information_id = information_id
        self.comment_user_id = comment_user_id
        self.create_user_id = create_user_id
        self.update_at = update_at
        self.create_at = create_at
        
    def as_dict(self):
        return {
            "id": self.id,
            "content": self.content,
            "information_id": self.information_id,
            "comment_user_id": self.comment_user_id,
            "create_user_id": self.create_user_id,
            "create_at": self.create_at,
           
        }


'''
点赞表
surpport_user_id = 点赞者id
information_id = 资讯id
create_user_id = 资讯发布者id
'''

@swagger.model
class Support(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    support_user_id = db.Column(db.Integer, nullable=False)
    information_id = db.Column(db.Integer, nullable=False)
    create_user_id = db.Column(db.Integer, nullable=False)
    update_at = db.Column(db.String(120))
    create_at = db.Column(db.String(120))

    def __init__(self, support_user_id, information_id,create_user_id,update_at, create_at):
        self.support_user_id = support_user_id
        self.information_id = information_id
        self.create_user_id = create_user_id
        self.update_at = update_at
        self.create_at = create_at

    def as_dict(self):
        return {
            "id": self.id,
            "support_user_id": self.support_user_id,
            "information_id": self.information_id,
            "create_user_id": self.create_user_id,
            "create_at": self.create_at,
        }
'''
踩表
surpport_user_id = 踩者id
information_id = 资讯id
create_user_id = 资讯发布者id
'''
@swagger.model
class UnSupport(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    unsupport_user_id = db.Column(db.Integer, nullable=False)
    information_id = db.Column(db.Integer, nullable=False)
    create_user_id = db.Column(db.Integer, nullable=False)
    update_at = db.Column(db.String(120))
    create_at = db.Column(db.String(120))

    def __init__(self, unsupport_user_id, information_id,create_user_id,update_at, create_at):
        self.unsupport_user_id = unsupport_user_id
        self.information_id = information_id
        self.create_user_id = create_user_id
        self.update_at = update_at
        self.create_at = create_at

    def as_dict(self):
        return {
            "id": self.id,
            "unsupport_user_id": self.unsupport_user_id,
            "information_id": self.information_id,
            "create_user_id": self.create_user_id,
            "create_at": self.create_at,
        }

'''
举报表
content = 内容
informaiton_id = 资讯id
report_user_id = 举报者id
create_user_id = 资讯发布者id
examine 
0 为 未审核

1 为 审核
'''



@swagger.model
class Report(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    content = db.Column(db.String(120))
    information_id = db.Column(db.Integer, nullable=False)
    report_user_id = db.Column(db.Integer, nullable=False)
    create_user_id = db.Column(db.Integer, nullable=False)
    examine_state = db.Column(db.Integer, nullable=False)
    update_at = db.Column(db.String(120))
    create_at = db.Column(db.String(120))

    def __init__(self, content, information_id,report_user_id,create_user_id,examine_state,update_at, create_at):
        self.content = content
        self.information_id = information_id
        self.report_user_id =report_user_id
        self.create_user_id = create_user_id
        self.examine_state = examine_state
        self.update_at = update_at
        self.create_at = create_at


    def as_dict(self):
        return {
            "id": self.id,
            "content": self.content,
            "information_id": self.information_id,
            "report_user_id": self.report_user_id,
            "create_user_id": self.create_user_id,
        }


if __name__ == "__main__":
    # 当运行这个py文件时，生成所有的表
    db.create_all()
    # db.session.add(
    #     Admin(
    #         account="zrnb",
    #         password="123456",
    #         isadmin=1,
    #         update_at=time.time(),
    #         create_at=time.time()
    #     )
    # )
    # db.session.add(
    #     Information(
    #     )
    # )
    db.session.commit()

    # print(Information.query.all())
